﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace Ling.P2P.DemoLib
{
    /// <summary>
    /// 用于表示客户端信息的实体类型。
    /// </summary>
    [Serializable]
    public class ClientInfo
    {
        private string _Name;
        private string _IPAddress;
        private int _Port;

        public ClientInfo()
        { }

        public ClientInfo(string name, string IP, int p)
        {
            _Name = name;
            _IPAddress = IP;
            _Port = p;
        }

        /// <summary>
        /// 获取或设置客户端别称。
        /// </summary>
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        /// <summary>
        /// 获取或设置客户端 IP 地址。
        /// </summary>
        public string IPAddress
        {
            get { return _IPAddress; }
            set { _IPAddress = value; }
        }

        /// <summary>
        /// 获取或设置客户端端口。
        /// </summary>
        public int Port
        {
            get { return _Port; }
            set { _Port = value; }
        }

        /// <summary>
        /// 转换成字符串显示。
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Name;
        }

        /// <summary>
        /// 将指定的 ClientInfo 对象序列化为字节数组。
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static byte[] Serialization(ClientInfo client)
        {
            if (client == null) return null;

            StringBuilder content = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            content.Append("<ClientInfo>");
            content.AppendFormat("<Name Value=\"{0}\" />", client.Name);
            content.AppendFormat("<IPAddress Value=\"{0}\" />", client.IPAddress);
            content.AppendFormat("<Port Value=\"{0}\" />", client.Port);
            content.Append("</ClientInfo>");

            byte[] data = Encoding.UTF8.GetBytes(content.ToString());
            return data;
        }

        /// <summary>
        /// 将指定的字节数组反序列化为 ClientInfo 对象。
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ClientInfo Deserialization(byte[] data)
        {
            string content = Encoding.UTF8.GetString(data);
            XmlDocument XDoc = new XmlDocument();
            XDoc.LoadXml(content);
            XmlNode n = XDoc.SelectSingleNode("/ClientInfo/Name");
            ClientInfo c = new ClientInfo();
            if (n != null)
                c.Name = n.Attributes["Value"].Value;
            n = XDoc.SelectSingleNode("/ClientInfo/IPAddress");
            if (n != null)
                c.IPAddress = n.Attributes["Value"].Value;
            n = XDoc.SelectSingleNode("/ClientInfo/Port");
            if (n != null)
                c.Port = string.IsNullOrEmpty(n.Attributes["Value"].Value) ? 0 : int.Parse(n.Attributes["Value"].Value);
            return c;
        }
    }
}
